﻿
Type.registerNamespace('AjaxControlToolkit');


AjaxControlToolkit.ButtonMenuBehavior = function(element) {
    /// <summary>
    /// The ButtonMenuBehavior is used to display a popup whenever the target is clicked
    /// </summary>
    /// <param name="element" type="Sys.UI.DomElement" domElement="true">
    /// DOM element the behavior is associated with
    /// </param>
    AjaxControlToolkit.ButtonMenuBehavior.initializeBase(this, [element]);
    
    this._click$delegate = Function.createDelegate(this, this._click);
    this._documentclick$delegate = Function.createDelegate(this, this._documentclick);
}


AjaxControlToolkit.ButtonMenuBehavior.prototype = {
    initialize : function() {
        /// <summary>
        /// Initialize the behavior
        /// </summary>
        AjaxControlToolkit.ButtonMenuBehavior.callBaseMethod(this, 'initialize');

        //add our handlers
        $addHandler(this.get_parentElement(), 'click', this._click$delegate);
        $addHandler(document, 'click', this._documentclick$delegate);
    }
    
    ,dispose : function() {
        /// <summary>
        /// Dispose the behavior
        /// </summary>    
        
        //remove our handlers
        $removeHandler(this.get_parentElement(), 'click', this._click$delegate);
        $removeHandler(document, 'click', this._documentclick$delegate);
        
        //free our variables
        this._click$delegate = null;
        this._documentclick$delegate = null;
        
        AjaxControlToolkit.ButtonMenuBehavior.callBaseMethod(this, 'dispose');
    }
    
    ,_click : function(event) {
        /// <summary>
        /// Raised when user click the button.
        /// Stops the propafation and prevents postback for button.
        /// </summary>
        event.preventDefault();
        event.stopPropagation();
        this.show();
    }
    
    ,_documentclick :function(event) {
        /// <summary>
        /// Raised when user click out from button.        
        /// Hide the popupmenu.
        /// </summary>     
        this.hide();
    }    
}    

AjaxControlToolkit.ButtonMenuBehavior.registerClass('AjaxControlToolkit.ButtonMenuBehavior', AjaxControlToolkit.PopupBehavior);
